 function ghvec=compare_irf_plots(irf_mat,shockpos,rownames,shonames,legends,outfold,in) 
% =========================================================================
% PLOT_IRFPERTURB.M 
%  
% ghvec=plot_irfperturb(irf_mat,stpos,shockpos,legends,stnames,shonames,addtotit) 
%
% Given a matrix of IRFS  IRFMAT of dimension [NST NPER NX NC] 
% Where the pages correspond to SHOCKS and the books to parameter
% perturbations, plot the IRF to the 
% shocks given in SHOCKPOS. Creating one figure for each SHOCK 
% 
% If STPOS (SHOCKPOS) not provided, whole matrix of states (shocks) will be produced 
% 
% Additional inputs 
% ------------------
% LEGENDS       Cell indicating which each line stands for 
% STNAMES       Cell with names of full length state vector (NSTx1) 
% SHONAMES      Cell with names of shocks (NX x 1 ) 
% ADDTOTIT      String to add to the SUPTITLE e.g. STORAGE 
% 
% Output
% GHVEC         Vector with the handles to each figure
%
% Alejandro Justiniano  January 30 2008 
% =========================================================================

% 1. Check dimensions for matrix and assign names to states, shocks and
% legends if not provided 
% =========================================================================
[nst nper nx nbook]=size(irf_mat);
if nargin < 2 || isempty(shockpos); 
    shockpos=(1:nx); 
end 
if nbook > 7;error('Only allow up to 7 perturbations in the same plot'); end 
shockpos=sort(shockpos);nxuse=length(shockpos); 
if nxuse > nx; error('SHOCKPOS cannot be greater than NX'); end 
% Check Other optional arguments 
% -------------------------------
if nargin < 6 || isempty(outfold); 
    flag_save=0; 
else 
    flag_save=1; 
    cucd=cd; 
end 
if nargin < 5 || isempty(legends)
    legends=fnumcell('Line',(1:nbook));
else
    if length(legends)~=nbook;error('LEGENDS does not match books in IRF_MAT');end
end
if nargin < 4 || isempty(shonames);
    shonames=fnumcell('Shock ',(1:nx)); 
else 
    if length(shonames)~=nx;error('SHONAMES does not match columns in IRF_MAT'); end 
end 
if nargin < 3 || isempty(rownames)
    rownames=fnumcell('St',(1:nst));
else
    if length(rownames)~=nst;error('STNAMES does not match rows in IRF_MAT');end
end

% 2. Define General Settings for the PLOTS 
% =========================================================================
%style ={'-','--','-.','-o','-*','--.',':*'}; 
width= [          1.5     2.5    2.5    2     2           2     ]; 
Style            ={'-','--','-.','--','-','--'};
Colors          =[0 0 1 ;1 0 0  ;0 0 0 ;1 0 1;0.75 0.75 0.75 ;0 0.8 0];
Marker          ={'none','none' ,'none','X'  ,'diamond'   ,'square'}; 
MarkerSize      =[5      2       2       7     3.5          4]; 
MarkerEdgeColor =Colors;
MarkerFaceColor =Colors; 
%colors={'b','r','k','g','m','b','r'}; 

% Colors=[0.1    0.1     0.1
%               0.35   0.35    0.35 
%              0.55   0.55    0.55
%              0.5    0.5     0.5 
%              0.4    0.4     0.4 
%              0.3    0.3     0.3 
%              0.5    0.5     0.5 ]; 
if nst <= 10;
    ncol=2;
    flag_landscape=0;
    titfont=12; 
    legfont=12; 
elseif nst <=15;
    ncol=3;
    flag_landscape=0; 
    titfont=11; 
    legfont=11; 
elseif nst <=20 
    ncol=5; 
    flag_landscape=1; 
    titfont=10; 
    legfont=10; 
elseif nst <=30 
    ncol=6; 
    flag_landscape=1; 
    titfont=9; 
    legfont=9; 
else 
    error('Cannot plot more than 30 IRFS in 1 graph'); 
end 
flag_legendoff=0; 
flag_addfilename=0; 
if nargin > 6 && ~isempty(in)
    if isfield(in,'ncol');
        ncol=in.ncol;
    end
    if isfield(in,'titfont');
        titfont=in.titfont;
    end
    if isfield(in,'legfont');
        legfont=in.legfont;             
    end
    if isfield(in,'legfont');
        flag_addpath=in.flag_addpath;             
    else 
        flag_addpath=0; 
    end 
    if isfield(in,'addcover');
        addcover=in.addcover;
    end
    if isfield(in,'flag_legendoff');
        flag_legendoff=in.flag_legendoff;
    end  
    [~,in]=ch_field(in,'yoffset',0.1*ones(nst,1));

    if isfield(in,'addfilename');
        flag_addfilename=1;                
    end  
    if isfield(in,'axfont');
        set(0,'DefaultAxesFontSize',in.axfont);
    end
    [~,in]=ch_field(in,'yoffset',0.1*ones(nst,1));
        
else 
    in.yoffset=0.1*ones(nst,1);
    flag_addpath=0; 
end 

nrow=ceil((nst/ncol));
xvec=(0:nper-1)';

if flag_save==1 && flag_addpath==1; 
    ghandle=figure; 
    hand=plot( zeros(10,1), zeros(10,1) ); 
    if flag_landscape==1; 
        set(ghandle,'PaperOrientation','Landscape');
        set(ghandle,'PaperPosition',[0.2 0.1 10.5 8.3]);
    else
        set(ghandle,'PaperPosition',[0.75 0.5 7.5 10])
    end 
    ylim([-10 10]); 
    text(-0.95,9,'Stored IN:','FontSize',14); 
    text(-0.95,7,outfold,'FontSize',10); 
    if exist('addcover','var'); 
        for ii=1:size(addcover,1); 
            text(-0.95,5-2*(ii-1),addcover{ii},'FontSize',14); 
        end 
    end 
    set(gca,'Box','On');
    set(gca,'YColor','w');
    set(gca,'XColor','w');
    set(gca,'Ytick',[]); 
    set(gca,'Xtick',[]); 
end 

% =========================================================================
% 3. One figure per shock, subfigure per state 
% =========================================================================
ghvec=zeros(nxuse,1);
for ii=1:nxuse;
    ghvec(ii)=figure;
    % One figure for each STATE 
    for jj=1:nst
        subhand=subplot(nrow,ncol,jj);
        % Plot IRFS to each perturbation of the Impulse responses
        for kk=1:nbook;
            temp=squeeze(irf_mat(jj,:,shockpos(ii),kk));
            hand=plot(xvec,temp,'LineWidth',width(kk));
            set(hand,'LineStyle',Style{kk},'Color',Colors(kk,:)); 
            set(hand,'Marker',Marker{kk},'MarkerSize',MarkerSize(kk)); 
            set(hand,'MarkerEdgeColor',Colors(kk,:),'MarkerFaceColor',Colors(kk,:));                         
            if kk==1;hold on;end
        end
        
        ymin=min(min(squeeze(irf_mat(jj,:,shockpos(ii),:))));
        ymax=max(max(squeeze(irf_mat(jj,:,shockpos(ii),:))));
        ymin=ymin*(1-sign(ymin)*in.yoffset(jj));
        ymax=ymax*(1+sign(ymax)*in.yoffset(jj));
        if ymax-ymin < 0.01; 
            offset=0.005; 
        else 
            offset=1e-4; 
        end 
        ylim([ymin-offset ymax+offset]); 
        xlim([xvec(1) xvec(end)+0.5]);         
        set(subhand,'Box','Off'); 
        set(subhand,'Xtick',round(linspace(0,xvec(end),4))); 
        hold off;
        %axis tight; 
        title(rownames{jj},'FontSize',titfont);
        %axis tight; 
    end
        suptitle( shonames{ shockpos(ii) }, 14 ); 

    if flag_landscape==1
        set(ghvec(ii),'PaperOrientation','Landscape');
        set(ghvec(ii),'PaperPosition',[0.2 0.1 10.5 8.3]);
    else
        set(ghvec(ii),'PaperPosition',[0.5,0.5,7.5,10])
        set(ghvec(ii),'PaperPositionMode','manual');
    end
    if flag_legendoff==0; 
    hand=legend([220 10 0.4 0.4],legends(:),'orientation','horizontal','FontSize',legfont);
    set(hand,'Box','off');
    end 
end 
if flag_save==1; 
    cd(outfold); 
    if flag_addfilename==0
        graphname='IRFs';
    else
        graphname=['IRFs ',in.addfilename];
    end
    
    if exist([graphname,'.ps'],'file');
        delete([graphname,'.ps'])
        if exist([graphname,'.pdf'],'file');
            delete([graphname,'.pdf'])
        end       
    end
    if flag_addpath==1; 
        print(ghandle,'-dpsc',graphname,'-append'); 
    end 
    
    for ii=1:nxuse
        print(ghvec(ii),'-dpsc',graphname,'-append'); 
    end 
    cd(cucd); 
end